use std::collections::HashMap;

type Collection = HashMap<String, Vec<String>>;

pub fn add_animal_to_section(animal: &str, section: &str, registry: &mut Collection) {
    // TODO: implement this function
    // al 's type &Vec<String>
    let al = registry
        .entry(section.to_string())
        .or_insert(vec![animal.to_string()]);
    if !((*al).contains(&(animal.to_string()))) {
        (*al).push(animal.to_string());
    }
}

pub fn get_animals_in_section(section: &str, registry: &Collection) -> Vec<String> {
    // TODO: implement this function
    let al = registry.get(section);
    match al {
        Some(i) => {
            let mut r: Vec<String> = (*i).iter().map(|i| i.to_string()).collect();
            r.sort();
            r
        }
        None => Vec::new(),
    }
}

pub fn get_all_animals_sorted(registry: &Collection) -> Vec<String> {
    // TODO: implement this function
    let mut r: Vec<String> = Vec::new();
    for (_s, al) in registry {
        let mut tmp: Vec<String> = (*al).iter().map(|i| i.to_string()).collect();
        r.append(&mut tmp);
    }
    r.sort();
    r
}
